// miniprogram/pages/activity_analyze/activity_analyze.js
var wxCharts = require('../../data/wxcharts.js');
var func = require('../../utils/util.js')
var db = wx.cloud.database()
var global = getApp().globalData;
var lineChart;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subject: ['分析', '活动'],
    click: 0,
    sex: [{
      name: '男生',
      data: 0
    }, {
      name: '女生',
      data: 0
    }, {
      name: '待明确',
      data: 0
    }, ],
    classPeople: [{
      name: 2019,
      data: 0
    }, {
      name: 2018,
      data: 0
    }, {
      name: 2017,
      data: 0
    }, {
      name: 2016,
      data: 0
    }, {
      name: 2015,
      data: 0
    }, {
      name: 2014,
      data: 0
    }, {
      name: 2013,
      data: 0
    }, ]
  },

  onlineCanvas: function (id, data) {
    var windowWidth = '',
      windowHeight = ''; //定义宽高
    try {
      var res = wx.getSystemInfoSync(); //试图获取屏幕宽高数据
      windowWidth = res.windowWidth * 0.90; //以设计图750为主进行比例算换
      windowHeight = res.windowWidth / 750 * 350 //以设计图750为主进行比例算换
      lineChart = new wxCharts({ //定义一个wxCharts图表实例
        animation: true, //是否有动画
        canvasId: id,
        type: 'pie',
        series: data,
        width: windowWidth,
        height: windowHeight,
        dataLabel: true,
      });
    } catch (e) {
      //如果获取失败
      console.log(e)
      func.hideLoading('加载失败')
    }
  },

  onCount: function (arr, i) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i].sex == '男') {
        this.data.sex[0].data++;
      } else if (arr[i].sex == '女') {
        this.data.sex[1].data++;
      } else {
        this.data.sex[3].data++;
      }
      switch (arr[i].myClass) {
        case 19:
          this.data.classPeople[0].data++;
          break;
        case 18:
          this.data.classPeople[1].data++;
          break;
        case 17:
          this.data.classPeople[2].data++;
          break;
        case 16:
          this.data.classPeople[3].data++;
          break;
        case 15:
          this.data.classPeople[4].data++;
          break;
        case 14:
          this.data.classPeople[5].data++;
          break;
        default:
          this.data.classPeople[6].data++;
          break;
      }
    }
    this.onlineCanvas('sex', this.data.sex)
    this.onlineCanvas('class', this.data.classPeople)
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    db.collection('activityInfo').doc(options.id).get().then(res => {
      this.onCount(res.data.joinInfo)
      this.setData({
        show: res.data.joinInfo.length ? false : true
      })
    }).catch(res => {
      func.hideLoading('加载失败')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})